let Mock = require("mockjs");
// const { default: store } = require("../store");
Mock.mock("/data/json", "get", require("./data.json"));
Mock.mock("/data/address", "get", require("./address.json"));
Mock.mock("/data/goodscart", "get", require("./goodscart.json"));
import Store from '@/store/index.js'

// 用户登录接口 登录验证
// 请求格式 getlink('/data/user','get/post',{data对象})；
// 获取登录信息与数据进行比对
let login = function(data) {
    // 注意options.body传过来的值为字符串，需要转换为对象形式！！！
    const dataobj = JSON.parse(data)
    for (let i in User) {

        if (dataobj.username == User[i].username && dataobj.password == User[i].password) {

            return ["success", User[i].id];

        } else {
            continue;
        }
    }
    return "error";

};
// 引入JSON文件,方便操作,增删改查
let User = require('./user.json');
let Usercart = require('./usercart.json');


Mock.mock("/data/user", "post", (options) => {
        // console.log(options.body);
        return login(options.body);
        // 返回值会作为data属性
    })
    // 注册购物车空间函数
let open = function() {

        let arr = [];
        Usercart.push(arr);
        console.log(Usercart);
    }
    // 用户注册接口
let register = function(data) {
        // 注意options.body传过来的值为字符串，需要转换为对象形式！！！
        const dataobj = JSON.parse(data);
        for (let i in User) {

            if (dataobj.username == User[i].username) {
                return "error";

            } else {
                continue;
            }
        }
        User.push(dataobj);
        // 为用户数据分配ID方便后续操作
        User[User.length - 1].id = User.length - 1;
        // 为注册用户分配购物车空间
        open();
        return "success";

    }
    // 请求格式getlink('/data/register','get/post',{data})
Mock.mock("/data/register", "post", (options) => {
        // console.log(options.body);

        return register(options.body);
        // 返回值会作为data属性
    })
    // 用户名查询
let checkuser = function(data) {
    let dataobj = JSON.parse(data);
    for (let item of User) {
        if (dataobj.username == item.username) {
            return "error";
        }
    }
    return "success";

}
Mock.mock("/data/checkuser", "get", (options) => {
        return (checkuser(options.body));
    })
    // 用户购物车更新
    // let update = function(data) {
    //     const dataobj = JSON.parse(data);
    //     console.log(dataobj);
    //     for (let item of Usercart[Store.state.userid]) {

//     }
// }
let update = function(data) {
    const dataobj = JSON.parse(data);

    if (Usercart[Store.state.userid].length == 0) {
        console.log('对了');
        const arr2 = [
            [dataobj.goodsid, dataobj.goodsnums]
        ]
        const obj2 = {};
        obj2.shopid = dataobj.shopid;
        obj2.goodslist = arr2;
        Usercart[Store.state.userid].push(obj2);
        console.log(Usercart);
        return "success";

    } else {
        for (let item of Usercart[Store.state.userid]) {
            // 写的是个锤子
            if (item.shopid == dataobj.shopid) {
                for (let i of item.goodslist) {
                    if (i[0] == dataobj.goodsid) {
                        if (dataobj.goodsnums > 0) {
                            i.pop();
                            i.push(dataobj.goodsnums);
                            console.log(Usercart);
                            return "success";
                        } else {
                            item.goodslist.splice(item.goodslist.indexOf(i), 1);
                            if (item.goodslist.length == 0) {
                                Usercart[Store.state.userid].splice(Usercart[Store.state.userid].indexOf(item), 1);
                            } else {
                                return "success";
                            }
                        }
                    }
                }
                const arr = [];
                arr.push(dataobj.goodsid);
                arr.push(dataobj.goodsnums);
                item.goodslist.push(arr);
                console.log(Usercart);
                return "success";
            }
        }
        const arry = [];
        arry.push(dataobj.goodsid);
        arry.push(dataobj.goodsnums);
        const arrx = [];
        arrx.push(arry);
        const obj = {};
        obj.shopid = dataobj.shopid;
        obj.goodslist = arrx;
        Usercart[Store.state.userid].push(obj);
        console.log(Usercart);
        return "success";
    }
}
Mock.mock("/data/update", "get", (options) => {
    /*
    传入参数:
    shopid:0
    shoplist:[1,2]
    */
    // console.log(Usercart[Store.state.userid].length);
    // bug 原因,mockjs json数据刷新后消失,vuex中的数据则保存在sessionstorage中导致获取Usercart[Store.state.userid]数组不存在.
    // console.log(options);
    if (Store.state.userid == -1) {
        console.log("error");
        return "error";
    } else {
        return update(options.body);
    }
})


// 获取用户购物车商品接口
// 数据处理函数
let getgoods = function(data) {
    const dataobj = JSON.parse(data);
    const x = dataobj.shopid;
    const y = dataobj.userid;
    for (let item of Usercart[y]) {
        if (item.shopid == x) {
            console.log(item.goodslist);
            return item.goodslist;
        }
    }
    return null;

}
Mock.mock('/data/goodscart', 'get', (options) => {
    return getgoods(options.body);
})